package cz.muni.fi.pb138.bookinfo;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.logging.Level;
import java.util.logging.Logger;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.google.zxing.BinaryBitmap;
import com.google.zxing.ChecksumException;
import com.google.zxing.FormatException;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.NotFoundException;
import com.google.zxing.Reader;
import com.google.zxing.Result;
import com.google.zxing.common.HybridBinarizer;

public class BarcodeScanner {

    /*
     * Example of running this class:
     *
     */
    public static void main(String[] args) {
        BarcodeScanner scanner = new BarcodeScanner();
        System.out.println("ISBN: " + scanner.decode("book2.gif"));
    }
    
    /*
     * Method decode(String fileName)
     *
     * @param fileName string with path to image @return ISBN String ISBN of decoded file @throws Runtime exception when occurs error
     */
    public String decode(String fileName) {
        String isbn;

        FileInputStream barCodeInputStream;
        Bitmap bmap;
        try {
            barCodeInputStream = new FileInputStream(fileName);
            bmap = BitmapFactory.decodeStream(barCodeInputStream);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(BarcodeScanner.class.getName()).log(Level.SEVERE, null, ex);
            throw new RuntimeException("File not found", ex);
        }


        LuminanceSource source = new RGBLuminanceSource(bmap);
        BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
        Reader reader = new MultiFormatReader();
        Result result;
        try {
            result = reader.decode(bitmap);
        } catch (NotFoundException ex) {
            Logger.getLogger(BarcodeScanner.class.getName()).log(Level.SEVERE, null, ex);
            throw new RuntimeException("Not found exception", ex);
        } catch (ChecksumException ex) {
            Logger.getLogger(BarcodeScanner.class.getName()).log(Level.SEVERE, null, ex);
            throw new RuntimeException("Checksum exception", ex);
        } catch (FormatException ex) {
            Logger.getLogger(BarcodeScanner.class.getName()).log(Level.SEVERE, null, ex);
            throw new RuntimeException("Wrong format of file", ex);
        }

        isbn = validateISBN(result.getText());
        return isbn;
    }

    /*
     * Method validateISBN(String code)
     *
     * @param code String with decoded code @throws Runtime exception when occurs error
     */
    public String validateISBN(String code) {
        String isbn = null;
        String prefix = code.substring(0, 3);
        if ("978".equals(prefix) || "979".equals(prefix)) {
            isbn = code.substring(3);
        } else {
            throw new RuntimeException("Barcode does not contain valid ISBN code");
        }
        return isbn;
    }
}